import { animation, AnimationClip, Sprite, SpriteFrame, UITransform } from "cc";
import ResourceManager from "../Runtime/ResourceManager";
import { StateMachine } from "./StateMachine";
import { sortSpriteFrame } from "../Utils";


export const ANIMATION_SPEED = 1 / 8;
export default class State {

    private animationClip: AnimationClip

    constructor(
        private fsm: StateMachine,
        private path: string,
        private wrapMode: AnimationClip.WrapMode = AnimationClip.WrapMode.Normal,
        private speed = ANIMATION_SPEED,
        private events:any[] =[]
    ) {
        this.init()

    }

    async init() {
        const promise = ResourceManager.Instance.loadDir(this.path);
        this.fsm.waitingList.push(promise)

        const spriteFrames = await promise

        this.animationClip = new AnimationClip();


        const track = new animation.ObjectTrack(); // 创建一个向量轨道
        track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame'); // 指定轨道路径，即指定目标对象为 "Foo" 子节点的 "position" 属性
        const frames: Array<[number, SpriteFrame]> = sortSpriteFrame(spriteFrames).map((item, index) => [this.speed * index, item])
        track.channel.curve.assignSorted(frames);

        // 最后将轨道添加到动画剪辑以应用
        this.animationClip.addTrack(track);
        this.animationClip.name = this.path

        this.animationClip.duration = frames.length * this.speed; // 整个动画剪辑的周期
        this.animationClip.wrapMode = this.wrapMode;


        //添加帧事件
        for (const event of this.events) {
            this.animationClip.events.push(event)
        }
        this.animationClip.updateEventDatas();
    }

    run() {
        if (this.fsm.playerAnimation.defaultClip === this.animationClip) {
            return
        }
        this.fsm.playerAnimation.defaultClip = this.animationClip;
        this.fsm.playerAnimation.play();
    }
}


